//
//  MoveViewController.m
//  GSG
//
//  Created by dllo on 15/4/30.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MoveViewController.h"
#import "AFNetworking.h"
#import "MoveCell.h"
#import "VideoViewController.h"
#import "UIImageView+WebCache.h"
#import "MJRefresh.h"
#import "MJRefreshFooterView.h"
#import "MJRefreshHeaderView.h"
#import "MBProgressHUD.h"
#import"singleSong.h"
@interface MoveViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSInteger number;
}
@property(nonatomic,retain)NSMutableArray *modelArr;
@property (nonatomic,retain)UITableView *moveTableView;
@property(nonatomic,retain)MBProgressHUD*HUD;
@end

@implementation MoveViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"MV";
        number = 1;
        self.modelArr = [NSMutableArray array];
    }
    
    return self;
}
- (void)data{
    
    NSString *str1 = [NSString stringWithFormat:@"http://v1.ard.tj.itlily.com/ttpod?a=getnewttpod&id=366&page=%ld",number];
    NSString *str2 = @"&app=ttpod&v=v7.7.0.2015012818&uid=&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=8.1.2&active=1&net=2&openudid=de3da938b2edbfc60f8a99d225d3fa93b9f9aadc&idfa=5B573856-7D24-475E-9673-33B14BF5623E&utdid=VP+JXmLCbp4DAPajlznU24QL&alf=201200";
    NSString *str = [NSString stringWithFormat:@"%@%@", str1, str2];

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary *dict = responseObject;
        
        NSMutableArray *arr = [dict objectForKey:@"data"];
        for (NSInteger i=0; i<arr.count; i++) {
            
            MusicModel *movie = [[MusicModel alloc] init];
            NSMutableDictionary *movieDic = arr[i];
            [movie setValuesForKeysWithDictionary:movieDic];
            [self.modelArr addObject:movie];
            [movie release];
            
            
        }
        [self.moveTableView reloadData];
        
        [_HUD hide:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        //        NSLog(@"No");
        NSLog(@"%@",error);
    }];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
       self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bujing.png"]];
    
    
    
    
    
    [self data];
    self.moveTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.view.frame.size.height * 60 / 667) style:UITableViewStylePlain];
    self.moveTableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.moveTableView];
    self.moveTableView.rowHeight = 100;
    
    self.moveTableView.dataSource = self;
    self.moveTableView.delegate = self;
    
    [self addFooter];
    //方式1.直接在View上show
    _HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    //_HUD.delegate = self;
    
    //常用的设置
    //小矩形的背景色
    _HUD.color = [UIColor blackColor];//这儿表示无背景
    //显示的文字
    _HUD.labelText = @"提示";
    //细节文字
    _HUD.detailsLabelText = @"正在加载内容";
    //是否有庶罩
    _HUD.dimBackground = YES;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.modelArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"reuse";
    MoveCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[[MoveCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
    }
    MusicModel *move = self.modelArr[indexPath.row];
    //    NSURL *url = [NSURL URLWithString:music.pic_url];
    //    [cell.pic_urlView sd_setImageWithURL:url];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSURL *url = [NSURL URLWithString:move.pic_url];
    [cell.pic_urlView sd_setImageWithURL:url];
    
       
    cell.mv_nameLabel.text = move.mv_name;
    cell.singerLabel.text = move.singer;
    
    return cell;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    VideoViewController *video = [[VideoViewController alloc] init];
    video.musicModel = self.modelArr[indexPath.row];
    
    [self.navigationController pushViewController:video animated:YES];
    [video release];
    
}
-(void)addHeader {
    [self.moveTableView addHeaderWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self.moveTableView reloadData];
            
            [self.moveTableView headerEndRefreshing];
        });
    }];
    [self.moveTableView headerBeginRefreshing];
}

-(void)addFooter {
    [self.moveTableView addFooterWithCallback:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            number++;
            [self data];
            [self.moveTableView reloadData];
            
            [self.moveTableView footerEndRefreshing];
        });
    }];
}
    
    


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
